package com.jd.finance.core.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.finance.core.mapper.ProductMapper;
import com.jd.finance.core.pojo.dto.LoanProductInfoDTO;
import com.jd.finance.core.pojo.dto.ProductBankListInfoDTO;
import com.jd.finance.core.pojo.dto.SuccessProductMoneyDTO;
import com.jd.finance.core.pojo.entity.Product;
import com.jd.finance.core.service.ProductService;
import com.jd.finance.result.BaseResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 贷款产品表 服务实现类
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-17
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Resource
    private ProductMapper productMapper;
    @Resource
    private AdminUtilsServiceImpl adminUtilsService;

    /**
     * 定位查询产品
     *
     * @param City 城市定位
     * @return
     */
    @Override
    public BaseResult getLoanListByCity(String City) {
        if (StringUtils.isBlank(City)) {
            log.error("地址信息为空");
            return BaseResult.error().message("地址信息为空");
        }
        List<Product> list = productMapper.selectLoanListByCity(City);
        return BaseResult.ok().data("list", list).message("获取产品列表成功");
    }

    /**
     * 获取贷款产品成交总金额前5名
     *
     * @param uid
     * @return
     */
    @Override
    public BaseResult getSumMoneyTopFivePro(Integer uid) {
        if (uid == null) {
            log.error("id为空!");
            return BaseResult.error().message("id为空");
        }
        List<SuccessProductMoneyDTO> dtoList = productMapper.selectProductMoney();
        if (dtoList == null && dtoList.size() == 0) {
            log.error("未查询贷款产品成交金额!");
            return BaseResult.error().message("贷款产品成交金额为0!");
        }
        return BaseResult.ok().data("list", dtoList);
    }


    /**
     * 根据产品名称模糊查询，查询产品说明，与不同银行的基本利率
     *
     * @param uid
     * @param proName
     * @return
     */
    @Override
    public BaseResult getLoanProductInfo(Integer uid, String proName) {
        if (uid == null) {
            log.error("传入参数为空C");
            return BaseResult.error().message("传入参数为空");
        }
        if (StringUtils.isBlank(proName)) {
            log.error("传入参数为空C");
            return BaseResult.error().message("传入参数为空");
        }
        if (!adminUtilsService.isBank(uid)) {
            log.error("无访问权限");
            return BaseResult.error().message("无访问权限");
        }
        LoanProductInfoDTO loanProductInfo = productMapper.getLoanProductInfo(proName);
        Map<String, Object> result = new HashMap<>();
        result.put("pid", loanProductInfo.getProName());
        result.put("psm", loanProductInfo.getProInfo());
        result.put("banks", loanProductInfo.getLoanProductInfoBankDTOList());

        return BaseResult.ok().data(result);
    }


    /**
     * 根据产品id查询产品信息
     *
     * @return
     */
    @Override
    public BaseResult getProductInfo(Integer pid) {
        if (pid == null) {
            log.error("产品id为空!");
            return BaseResult.error().message("产品id为空！");
        }
        ProductBankListInfoDTO productBankListInfoDTO = productMapper.selectProductBankInfo(pid);

        if (productBankListInfoDTO == null) {
            log.error("未查询到该产品的相关信息!");
            return BaseResult.error().message("未查询到该产品的相关信息!");
        }

        Map<String, Object> result = new HashMap<>();
        result.put("pid", productBankListInfoDTO.getPid());
        result.put("cpname", productBankListInfoDTO.getProName());
        result.put("cpshuoming", productBankListInfoDTO.getProInfo());
        result.put("guanjianci", productBankListInfoDTO.getProKey());
        result.put("proBankInfo", productBankListInfoDTO.getProductBankInfoDTO());

        return BaseResult.ok().data(result);
    }


}
